package com.tianqu.tms.www.transfer.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.controller.TransferRgInputResultController;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;
/**
 * 
 * 进入过户登记录入结果页面Action
 * 
 * @author Administrator
 *
 */
public class TransferRgInputResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(TransferRgInputResultAction.class);
		
	private static final String SYSPATH = "sysPath";
	
	private static final String LEFTLINE ="/";
	
	private static final String TMSPATH = "/tmsweb";
	
	private static final String RESERIAL = "reSerial";
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String saveBsFlag = (String)uc.getAttribute("saveBsFlag");
		String reFlag = "";
		String forward = "";
		reFlag = (String)uc.getAttribute("reFlag");
		if(!"2".equals(saveBsFlag)&&!"3".equals(saveBsFlag))
		{
			TransferRgInputViewHelper view = (TransferRgInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.findTransStatusBySerial(view.getSerial());
			if (accept != null) {
				if(accept.getMaterialImages1()!=null && !"".equals(accept.getMaterialImages1()))
				{
					view.setMaterialImages1(accept.getMaterialImages1());
					view.setMaterialImages2(accept.getMaterialImages2());
					view.setMaterialImages3(accept.getMaterialImages3());
					view.setMaterialImages4(accept.getMaterialImages4());
					view.setMaterialImages5(accept.getMaterialImages5());						
				}else{
					setErrorsToRequest(request, TmsConstants.IM007, null);
					return mapping.getInputForward();
				}
			} else {
				setErrorsToRequest(request, TmsConstants.IM007, null);
				return mapping.getInputForward();
			}
		}

		//fix 20160115
//		if(reFlag == null || reFlag == ""){
//			TransferRgInputViewHelper view = (TransferRgInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
//			if(!"1".equals(view.getSaPicFlag())){
//				if(!"3".equals(view.getSourceType())){
//					if("".equals(view.getOwnersGroupPhoto())){
//						setErrorsToRequest(request, TmsConstants.IM042, null);
//						return mapping.getInputForward();
//					}
//				}
//			}
//		}
		//workload:3.1.4
//		if(reFlag != null && reFlag != ""){
//			TransferAcceptedInfo accept1 = new TransferAcceptedInfo();
//			String licensePlate = "";
//			TransferRgInputViewHelper view = (TransferRgInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
//			licensePlate = accept1.findReCar(view.getSerial());
//
//			if(licensePlate != null && licensePlate != ""){
//				forward = "toBack";
//				uc.setAttribute("errorFlag", licensePlate);
//				return mapping.findForward(forward);
//			}
//		}
		isToken(mapping, request);
		TransferRgInputResultController controller = new TransferRgInputResultController();
		String sysPath = request.getSession().getServletContext().getRealPath(LEFTLINE).replace(TMSPATH, TmsConstants.BLANK);
		uc.setAttribute(SYSPATH, sysPath);
		controller.execute(uc);
	
		String updateButtonBsFlag = (String)uc.getAttribute("updateButtonBsFlag");
		uc.removeAttribute("updateButtonBsFlag");
		uc.removeAttribute("saveBsFlag");
		if(("1".equals(saveBsFlag)||"3".equals(saveBsFlag))&&!"2".equals(updateButtonBsFlag)) 
		{
			TransferRgInputViewHelper view = (TransferRgInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			view.setPrintBsflag("1");
			Codes code = Codes.getInstance();
			if(view.getTransferType().equals("1"))
			{
				view.setTransferType1("√");
			}
			if(view.getTransferType().equals("2"))
			{
				view.setTransferType2("√");
			}
			if(view.getTransferType().equals("3"))
			{
				view.setTransferType3("√");
			}
			if(view.getTransferType().equals("4"))
			{
				view.setTransferType4("√");
			}
			if(view.getTransferType().equals("5"))
			{
				view.setTransferType5("√");
			}
//			1	非营运1
//			2	营转非19
//			3	警用17
//			4	消防14
//			5	救护15
//			6	工程救险16
//			7	幼儿校车8
//			8	小学生校车9
//			9	接送中小学生10
//			10	出租转非18
//			11	公路客运2
//			12	公交客运3
//			13	出租客运4
//			14	旅游客运5
//			15	租赁6
//			16	教练7
//			17	货运13
//			18	危险化学品运输 12
//			19	接送初中生11
			if(view.getCarUse().equals("1"))
			{
				view.setCarUse1("√");
			}	
			if(view.getCarUse().equals("2"))
			{
				view.setCarUse19("√");
			}	
			if(view.getCarUse().equals("3"))
			{
				view.setCarUse17("√");
			}	
			if(view.getCarUse().equals("4"))
			{
				view.setCarUse14("√");
			}	
			if(view.getCarUse().equals("5"))
			{
				view.setCarUse15("√");
			}	
			if(view.getCarUse().equals("6"))
			{
				view.setCarUse16("√");
			}	
			if(view.getCarUse().equals("7"))
			{
				view.setCarUse8("√");
			}	
			if(view.getCarUse().equals("8"))
			{
				view.setCarUse9("√");
			}	
			if(view.getCarUse().equals("9"))
			{
				view.setCarUse10("√");
			}	
			if(view.getCarUse().equals("10"))
			{
				view.setCarUse18("√");
			}	
			if(view.getCarUse().equals("11"))
			{
				view.setCarUse2("√");
			}	
			if(view.getCarUse().equals("12"))
			{
				view.setCarUse3("√");
			}	
			if(view.getCarUse().equals("13"))
			{
				view.setCarUse4("√");
			}	
			if(view.getCarUse().equals("14"))
			{
				view.setCarUse5("√");
			}	
			if(view.getCarUse().equals("15"))
			{
				view.setCarUse6("√");
			}	
			if(view.getCarUse().equals("16"))
			{
				view.setCarUse7("√");
			}	
			if(view.getCarUse().equals("17"))
			{
				view.setCarUse13("√");
			}	
			if(view.getCarUse().equals("18"))
			{
				view.setCarUse12("√");
			}
			if(view.getCarUse().equals("19"))
			{
				view.setCarUse11("√");
			}
			
			view.setCardTypeName(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, Integer.parseInt(view.getCardType())));

			request.setAttribute(TmsConstants.VIEWHELP, view);
			forward = "currentPage";
		}else{
			//跳转到首页
			forward = "topPage";
		}
		
//		String nereFlag = "";
//		nereFlag = (String)uc.getAttribute("nereFlag");
//		String reSerial = "";
//		if(nereFlag != null && nereFlag != ""){
//			List<TransferAcceptedInfo> reAcList = new ArrayList<TransferAcceptedInfo>();
//			reAcList = (List)uc.getAttribute("reAcList");
//			int i = 1;
//			if(reAcList!=null){
//				for(TransferAcceptedInfo entity : reAcList){
//					if(i == Integer.parseInt(nereFlag)+1){
//					   reSerial = entity.getSerial();
//					}
//					i++;
//				}
//			}
//			uc.setAttribute("reSerial", reSerial);
//			uc.setAttribute("nereFlag", String.valueOf(Integer.parseInt(nereFlag)+1));
//			forward = "toAgain";
//		}else
//		if(reFlag != null && reFlag != ""){
//			List<TransferAcceptedInfo> reAcList = new ArrayList<TransferAcceptedInfo>();
//			reAcList = (List)uc.getAttribute("reAcList");
//			int i = 1;
//			for(TransferAcceptedInfo entity : reAcList){
//				if(i == Integer.parseInt(reFlag)+1){
//				   reSerial = entity.getSerial();
//				}
//				i++;
//			}
//			uc.setAttribute("reSerial", reSerial);
//			uc.setAttribute("nereFlag", String.valueOf(Integer.parseInt(reFlag)+1));
//			forward = "toAgain";
//		}
//		
//		if("".equals(reSerial) && reFlag != null && reFlag != ""){
//			forward = "toBack";
//		}
		uc.removeAttribute(TmsConstants.VIEWHELP);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(forward);
	}

}
